#include "GlutCallbacks.h"

#include "GL/glut.h"

/*
	GlutCallbacks
*/

/*	private	*/

GlutCallbacks* GlutCallbacks::_instance = 0;

/*	public	*/

GlutCallbacks::GlutCallbacks() {
	_displayListener	 = 0;
	_idleListener		 = 0;
	_keyboardListener	 = 0;
	_motionListener		 = 0;
	_mouseListener		 = 0;
	_reshapeListener	 = 0;
	_specialListener	 = 0;

	glutDisplayFunc(GlutCallbacks::callbackDisplay);
	glutIdleFunc(GlutCallbacks::callbackIdle);
	glutKeyboardFunc(GlutCallbacks::callbackKeyboard);
	glutMotionFunc(GlutCallbacks::callbackMotion);
	glutMouseFunc(GlutCallbacks::callbackMouse);
	glutReshapeFunc(GlutCallbacks::callbackReshape);
	glutSpecialFunc(GlutCallbacks::callbackSpecial);
}

void GlutCallbacks::registerDisplayListener(GlutCallbackListener* listener) {
	_displayListener = listener;
}

void GlutCallbacks::registerIdleListener(GlutCallbackListener* listener) {
	_idleListener = listener;
}

void GlutCallbacks::registerKeyboardListener(GlutCallbackListener* listener) {
	_keyboardListener = listener;
}

void GlutCallbacks::registerMotionListener(GlutCallbackListener* listener) {
	_motionListener = listener;
}

void GlutCallbacks::registerMouseListener(GlutCallbackListener* listener) {
	_mouseListener = listener;
}

void GlutCallbacks::registerReshapeListener(GlutCallbackListener* listener) {
	_reshapeListener = listener;
}

void GlutCallbacks::registerSpecialListener(GlutCallbackListener* listener) {
	_specialListener = listener;
}

void GlutCallbacks::callbackDisplay() {
	GlutCallbackListener* listener = getInstance()->_displayListener;
	if (listener != 0) {
		listener->callbackDisplay();
	}
}

void GlutCallbacks::callbackIdle() {
	GlutCallbackListener* listener = getInstance()->_idleListener;
	if (listener != 0) {
		listener->callbackIdle();
	}
}

void GlutCallbacks::callbackKeyboard(unsigned char key, int x, int y) {
	GlutCallbackListener* listener = getInstance()->_keyboardListener;
	if (listener != 0) {
		listener->callbackKeyboard(key, x, y);
	}
}

void GlutCallbacks::callbackMotion(int x, int y) {
	GlutCallbackListener* listener = getInstance()->_motionListener;
	if (listener != 0) {
		listener->callbackMotion(x, y);
	}
}

void GlutCallbacks::callbackMouse(int button, int state, int x, int y) {
	GlutCallbackListener* listener = getInstance()->_mouseListener;
	if (listener != 0) {
		listener->callbackMouse(button, state, x, y);
	}
}

void GlutCallbacks::callbackReshape(int width, int height) {
	GlutCallbackListener* listener = getInstance()->_reshapeListener;
	if (listener != 0) {
		listener->callbackReshape(width, height);
	}
}

void GlutCallbacks::callbackSpecial(int key, int x, int y) {
	GlutCallbackListener* listener = getInstance()->_specialListener;
	if (listener != 0) {
		listener->callbackSpecial(key, x, y);
	}
}

GlutCallbacks* GlutCallbacks::getInstance() {
	if (_instance == 0) {
		_instance = new GlutCallbacks();
	}

	return _instance;
}

